package com.yisu.expressway.onedollar.presenter.impl;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.model.OrderDetailObj;
import com.yisu.expressway.onedollar.model.OrderObj;
import com.yisu.expressway.onedollar.presenter.IOrderDetailPresenter;
import com.yisu.expressway.onedollar.view.IOrderDetailView;

import android.content.Context;

/**
 * Created by shijunfeng on 2016/12/13.
 */
public class OrderDetailPresenterImpl implements IOrderDetailPresenter {
    private Context mContext;
    private IOrderDetailView mView;
    private OrderDetailObj mOrderDetailObj;

    public OrderDetailPresenterImpl(Context context, IOrderDetailView iOrderDetailView) {
        mContext = context;
        mView = iOrderDetailView;
    }

    @Override

    public void init() {

    }

    @Override
    public void fetchOrderDetailInfo(long orderId) {
        if (orderId <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        params.put("orderId", orderId);
        ApiClientHelper.postApi(ApiSetting.getOrderDetail(), new TypeToken<OrderDetailObj>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<OrderDetailObj>>() {
            @Override
            public void onResponse(ApiResult<OrderDetailObj> result) {
                if (!result.success) {
                    mView.showToast(result.getMsg());
                    mView.refreshView(false);
                    return;
                }
                mOrderDetailObj = result.getResult();
                mView.refreshView(true);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mView.showToast(volleyError.getMessage());
                mView.refreshView(false);
            }
        }, mContext);
    }

    @Override
    public OrderDetailObj getOrderDetailInfo() {
        return mOrderDetailObj;
    }

}
